package dtd.PHS.VND_ExchangeRate.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.graphics.LinearGradient;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import dtd.PHS.VND_ExchangeRate.R;
import dtd.PHS.VND_ExchangeRate.data.ListRates;
import dtd.PHS.VND_ExchangeRate.data.RateItem;
import dtd.PHS.VND_ExchangeRate.data.SJCItem;
import dtd.PHS.VND_ExchangeRate.ui.adapters.RatesAdapter;
import dtd.PHS.VND_ExchangeRate.utils.Helpers;

public abstract class ListRatesView 
	extends RequestableFrameLayout 
{

	private static final int DELAY_TIME = 1000;
	private ListView listview;
	private TextView tvBuying;
	private TextView tvSelling;
	private TextView tvUpdateTime;
	private LinearLayout layoutGold;

	public ListRatesView(Activity activity) {
		super(activity.getApplicationContext());
	}


	@Override
	public void onGetDataError(Exception e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void createViews() {	    
		Helpers.inflateView(R.layout.list_rates_act,this);
		main_frames = (FrameLayout) findViewById(R.id.main_frames);
		createListView();
		createSJCViews();
	}


	private void createListView() {
		listview = (ListView) findViewById(R.id.lvListRates);		
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				showShareDialog((RateItem) (adapter.getItem(position)));
			}
		});
	}


	private void createSJCViews() {
		layoutGold = (LinearLayout) findViewById(R.id.layoutGold);
		layoutGold.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if ( goldPrice != null )
					showShareGoldPriceDialog(goldPrice);
			}			
		});
		
		tvBuying = (TextView) findViewById(R.id.tvBuyingRate);
		tvSelling = (TextView) findViewById(R.id.tvSellingRate);
		tvUpdateTime = (TextView) findViewById(R.id.tvUpdateTime);
	}

	abstract public void showShareGoldPriceDialog(SJCItem goldPrice);
	abstract public void showShareDialog(RateItem rateItem);
	private ListRates listRates;
	private RatesAdapter adapter;
	private SJCItem goldPrice;
	
	@Override
	public void onGetDataCompleted(Object data) {
		if ( data instanceof ListRates) {
//			Delayer delayer = new Delayer(DELAY_TIME) {
//				@Override
//				public void runBefore() {
//					showOnlyView(WAITING_FRAME);
//				}
//
//				@Override
//				public void runAfter() {
//					showOnlyView(DATA_FRAME);
//				}
//
//			};
//			delayer.start();
			refreshListView(data);			
		} else if ( data instanceof SJCItem) {
			goldPrice = (SJCItem) data;
			tvBuying.setText(Helpers.formatGoldPrice(goldPrice.getBuying()));
			tvSelling.setText(Helpers.formatGoldPrice(goldPrice.getSelling()));
			updateSJCUpdateTime(goldPrice);
		}
	}


	private void updateSJCUpdateTime(SJCItem price) {
		Date updateTime = price.getUpdateTime();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm aa");
		tvUpdateTime.setText(" " + sdf.format(updateTime));
	}

	private void refreshListView(Object data) {
		listRates = (ListRates) data;
		adapter = new RatesAdapter( this.getContext(),listRates );
		listview.setAdapter(adapter);
		showOnlyView(DATA_FRAME);
	}


}
